/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop.config;

import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

/**
 * {@code NamespaceHandler} for the {@code aop} namespace.
 *
 * <p>Provides a {@link org.springframework.beans.factory.xml.BeanDefinitionParser} for the
 * {@code <aop:config>} tag. A {@code config} tag can include nested
 * {@code pointcut}, {@code advisor} and {@code aspect} tags.
 *
 * <p>The {@code pointcut} tag allows for creation of named
 * {@link AspectJExpressionPointcut} beans using a simple syntax:
 * <pre class="code">
 * &lt;aop:pointcut id=&quot;getNameCalls&quot; expression=&quot;execution(* *..ITestBean.getName(..))&quot;/&gt;
 * </pre>
 *
 * <p>Using the {@code advisor} tag you can configure an {@link org.springframework.aop.Advisor}
 * and have it applied to all relevant beans in you {@link org.springframework.beans.factory.BeanFactory}
 * automatically. The {@code advisor} tag supports both in-line and referenced
 * {@link org.springframework.aop.Pointcut Pointcuts}:
 *
 * <pre class="code">
 * &lt;aop:advisor id=&quot;getAgeAdvisor&quot;
 *     pointcut=&quot;execution(* *..ITestBean.getAge(..))&quot;
 *     advice-ref=&quot;getAgeCounter&quot;/&gt;
 *
 * &lt;aop:advisor id=&quot;getNameAdvisor&quot;
 *     pointcut-ref=&quot;getNameCalls&quot;
 *     advice-ref=&quot;getNameCounter&quot;/&gt;</pre>
 *
 * @author Rob Harrop
 * @author Adrian Colyer
 * @author Juergen Hoeller
 * @since 2.0
 */
public class AopNamespaceHandler extends NamespaceHandlerSupport {

	/**
	 * Register the {@link BeanDefinitionParser BeanDefinitionParsers} for the
	 * '{@code config}', '{@code spring-configured}', '{@code aspectj-autoproxy}'
	 * and '{@code scoped-proxy}' tags.
	 */
	/*
		跟踪ClassPathXmlApplicationContext类到
		refresh()-->obtainFreshBeanFactory()-->refreshBeanFactory()-->loadBeanDefinitions()
		-->XmlBeanDefinitionReader了类的loadBeanDefinitions(EncodedResource encodedResource)
		-->在delegate.parseCustomElement(ele)解析自定义标签这个方法中-->.......
		-->org.springframework.beans.factory.xml.BeanDefinitionParserDelegate中的
		parseCustomElement(Element ele, @Nullable BeanDefinition containingBd)方法中，
		里面有一个调用的方法是
		NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
		在调用resolve(namespaceUri)方法的时候：通过namespaceUri（命名空间uri），并通过反射来获取执行的NamespaceHandler
		并调用init()方法 来创建标签对应的解析器，同时将NamespaceHandler放入到handlerMappings的map集合中
		最后在调用handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));的方法
		来执行BeanDefinitionParser中的parse()方法
	 */
	@Override
	public void init() {
		// In 2.0 XSD as well as in 2.1 XSD.
		// spring aop 学习网址：
		// https://juejin.im/post/5c1c402b6fb9a049a570df27  里面讲述了AOP使用到的一些设计模式
		//
		// https://www.cnblogs.com/xrq730/p/6753160.html
		// https://www.cnblogs.com/xrq730/p/6757608.html      整个流程讲解
		/*
			从registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());源代码中可以看出
			1、<aop:config>对应 spring 默认给我们创建了BeanName为org.springframework.aop.config.internalAutoProxyCreator的
			类为AspectJAwareAdvisorAutoProxyCreator.class的RootBeanDefinition，
			并注册到DefaultListableBeanFactory中去，
			2、<aop:pointcut>对应的类为AspectJExpressionPointcut.class
			3、<aop:aspect>对应的类为未知
			4、<aop:before>对应的类为AspectJMethodBeforeAdvice.class
			5、<aop:after>对应的类为AspectJAfterAdvice.class
		 */
		registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
		registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
		registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());

		// Only in 2.0 XSD: moved to context namespace as of 2.1
		registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
	}

}
